extends RigidBody2D

@export var speed:int=100
@export var hp:int=3
@export var gravityScale:float=1.5

var point:int=0
var screen_size

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

func _ready() -> void:
	screen_size=get_viewport_rect().size
	
	linear_velocity=Vector2(speed,0)
	set_linear_damp(0)
	gravity_scale=gravityScale
	
	contact_monitor=true  #必须设置，要不然不会触发碰撞检测
	max_contacts_reported=1
#	
	connect("body_entered",Callable(self,"on_body_entered"))
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if rotation<deg_to_rad(-30):
		rotation=deg_to_rad(-30)
		angular_velocity=0
	if linear_velocity.y>0:
		angular_velocity=3
	
	if Input.is_action_just_pressed("fly_button"):
		linear_velocity=Vector2(speed,-200)
		angular_velocity=-3
		gravity_scale=0
		$AnimatedSprite2D.play()
		$Wing.play()
		
	if $AnimatedSprite2D.frame==2:
		$AnimatedSprite2D.stop()
		$AnimatedSprite2D.frame=0
		gravity_scale=gravityScale
				 
	position.y=clamp(position.y,0,screen_size.y)
	pass


func on_body_entered(body: Node) -> void:
	$Hit.play()
	hp-=1
	pass # Replace with function body.
